package com.example.leo.mycharmingstudent.a0831;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

public class MainPageActivity extends AppCompatActivity {

    //ctrl+alt+c 定义常量
    public static final String MINE_FRAGMENT = "mineFragment";
    //fragment容器
    private FrameLayout flContainer;
    //底部菜单标签
    private TextView tvMenu;
    //底部我的标签
    private TextView tvMine;
    private MenuFragment mMenuFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page_activity);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        flContainer = findViewById(R.id.fl_container_main_page_activity);
        tvMenu = findViewById(R.id.tv_menu_main_page_activity);
        tvMine = findViewById(R.id.tv_mine_main_page_activity);
    }

    private void initData() {
        mMenuFragment = new MenuFragment();
        /*
        * 向Fragment传递参数
        * 第一步，new Bundle
        * 第二步，bundle对象的putString等方法
        * 第三步，setArguments
        * */
        Bundle bundle = new Bundle();
        bundle.putString("title", "传值的菜单Fragment标题");
//        bundle.putInt();
        mMenuFragment.setArguments(bundle);


        mMineFragment = new MineFragment();
        Bundle mineFragmentBundle = new Bundle();
        mineFragmentBundle.putString("title", "传值的我的Fragment标题");
        mMineFragment.setArguments(mineFragmentBundle);

        //Fragment使用流程
        //第一步获取manager
        FragmentManager manager = getSupportFragmentManager();
        //第二步开启事务,获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //第三步  执行FragmentTransaction add hide show方法
        transaction.add(
                R.id.fl_container_main_page_activity,
                mMenuFragment,
                "menuFragment");
        transaction.add(
                R.id.fl_container_main_page_activity,
                mMineFragment,
                "mineFragment");
        transaction.hide(mMineFragment);
        //最后一步，第四步，提交
        transaction.commit();

        MineFragment fragment = (MineFragment) getSupportFragmentManager()
                .findFragmentByTag(MINE_FRAGMENT);

    }

    private void initEvent() {
        //菜单标签点击事件
        tvMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换到MenuFragment
                switchMenuFragment();
            }
        });

        //我的标签点击事件
        tvMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换到MineFragment
                switchMineFragment();
            }
        });
    }

    /**
     * 显示MineFragment
     */
    private void switchMineFragment() {
        //此处Fragment流程注释，参考initData方法
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(mMenuFragment);
        transaction.show(mMineFragment);
        transaction.commit();
    }

    /**
     * 显示MenuFragment
     */
    private void switchMenuFragment() {
        //此处Fragment流程注释，参考initData方法
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(mMineFragment);
        transaction.show(mMenuFragment);
        transaction.commit();
    }
}
